function Marque($){
	$.fn.marque = function(option,genCallBack){
		if(this){
			var container = null;
			if(this.length > 1){
				container = this[0];
			}else{
				container = this;
			}
			return marquePlugin(container,option,genCallBack);
		}
	}
	
	function marquePlugin(container,option,genCallBack){
		
		var business = {
			/**
			 * 以项目为单位的滚动
			 * container:容器对象(容器对象必须为ul)
			 * opition{
			 * 	  data:数据列表
			 * 		{
			 * 			title:
			 * 			link:
			 * 		}
			 *    stepTimespan:单步时间间隔
			 *    stepHeight:单步滚动的高度
			 *    itemSteps:项目的步数
			 *    itemStop:1 停留 0 不停留
			 * }
			 * genCallBack：项目内容生成回调(回调建议使用)
			 */
			init:function(container,option,genCallBack){
				var _this = this;
				if(container&&option){
					_this.container = $(container);
					_this.option = {stepTimespan:20,itemSteps:20,itemStop:1};
					$.extend(_this.option,option||{});///设置滚动选项
					//存在数据时
					if(_this.option.data&&_this.option.data.length>0){
						//存在定义生成方法，使用自定义方法
						if(genCallBack || typeof genCallBack ==="function"){
							_this.genCallBack = genCallBack;
						}
						_this.curIdx = 1;
						_this.fillScrollOption();
						_this.renderItems();
						_this.startMarquee();
					}
				}
				return _this;
			},
			//填充滚动选项
			fillScrollOption:function(){
				var _this = this,containerHeight = $(this.container).height();
				if(_this.option.stepHeight && _this.option.stepHeight*_this.option.itemSteps <= containerHeight){
					return;
				}
				_this.option.stepHeight = containerHeight/_this.option.itemSteps;
				_this.option.itemStopTime = _this.option.itemStop==1?1000:_this.option.stepTimespan;
			},
			//获得滚动项目数组
			getItemScrollArray:function(){
				var _this = this,startIdx = _this.curIdx - 1,endIdx = _this.curIdx,spanArray = [];
				if(_this.option.data.length==1){
					spanArray.push(_this.option.data[startIdx]);
				}else{
					if(endIdx==_this.option.data.length){
						endIdx = 0;
					}
					spanArray.push(_this.option.data[startIdx]);
					spanArray.push(_this.option.data[endIdx]);
				}
				return spanArray;
			},
			//展示列表
			renderItems:function(){
				var _this = this,spanArray = _this.getItemScrollArray(),childrens = _this.container.children("li"),isGened = true;
				$.each(spanArray,function(idx){
					if(childrens.length>0){
						if(_this.genCallBack){
							$(childrens[idx]).html(_this.genCallBack.call(null,this,_this));
						}else{
							$(childrens[idx]).html(this.title);
						}
					}else{
						isGened = false;
						if(_this.genCallBack){
							$("<li>").html(_this.genCallBack.call(null,this,_this)).appendTo(_this.container);
						}else{
							$("<li>").html(this.title).appendTo(_this.container);
						}
					}
				});
				if(!_this.container[0]["onmouseout"]){
					_this.container.mouseout(function(){
						_this.marqueeStep();
					});
					_this.container.mouseover(function(){
						_this.stepInterval = clearInterval(_this.stepInterval);
						_this.itemInterval = clearInterval(_this.itemInterval);
					});
				}
			},
			//开始滚动
			startMarquee:function(){
				var _this = this;
				if(_this.container.children("li").length>1){
					setTimeout(function(){
						_this.marqueeStep();
					},_this.option.itemStopTime);
				}
			},
			//项目滚动时处理
			marqueeItem:function(){
				var _this = this;
				if(_this.itemInterval) return;
				var pauseTime = _this.option.itemStop==1?1000:_this.option.stepTimespan;
				_this.itemInterval = setInterval(function(){
					_this.container.scrollTop(0);
					if(_this.curIdx==_this.option.data.length){
						_this.curIdx = 1;
					}else{
						_this.curIdx = _this.curIdx + 1;
					}
					_this.renderItems();
					_this.itemInterval = clearInterval(_this.itemInterval);
					_this.marqueeStep();
				},_this.option.itemStopTime);
			},
			//单步滚动时处理
			marqueeStep:function(){
				var _this = this,idx = 0;
				if(_this.stepInterval)return;
				_this.stepInterval = setInterval(function(){
					idx++;
					_this.container.scrollTop(idx*_this.option.stepHeight);
					if(idx%_this.option.itemSteps==0){ //滚完设定的步数时
						_this.stepInterval = clearInterval(_this.stepInterval);
						_this.marqueeItem();
						return;
					}
				},_this.option.stepTimespan);
			}
		};
		business.init(container,option,genCallBack);
		return business;
	}

}


if("function"==typeof define && define.amd && define){
	define(['jquery'],function($){
		return Marque($);
	});
}